<?php
    
    function userGET($request){
		
		$user = new User();
		
		$getById = false;
		if (!(!$request->getArgs || $request->getArgs['id'] == "")) {
			$user->setUserId($request->getArgs['id']);
			$getById = true;
		}
			
		$userBusiness = BusinessFactory::factory('UserBusiness');
		$result = $userBusiness->select($user);
		
		if ($result != NULL) {
		
			if(is_array($result)) {
				if($getById) {
					if (sizeof($result) == 1) {
						header("Status: 200 Success", true, 200);
						return $result[0];
					}
					else {
						header("Status: 404 Not Found", true, 404);
					}
				}
				else {
					header("Status: 200 Success", true, 200);
					return $result;
				}
			} 
			else {
				header("Status: 500 Internal Server Error", true, 500);
			}
		}
		else {
			header("Status: 500 Internal Server Error", true, 500);
		} 
		
		return $result;
    }
    
	function userPOST($request){
			
		$userJSON = json_decode(utf8_decode($request->postArgs['user']));
		
		$user = new User();
		
		$user->JSONToObject($userJSON);

		$userBusiness = BusinessFactory::factory('UserBusiness');
		$result = $userBusiness->save($user);

		if ($result != -1) {
			
			header("Status: 201 Created", true, 201);
			header("Location: " . $request->getActualUrl() . $result);
		}
		else {
			header("Status: 500 Internal Server Error", true, 500);
		}
	}
	
	function userrequestGET($request) {
		
		$user = new User();
		$user->setUserId($request->getArgs['id']);
			
		$requestBusiness = BusinessFactory::factory('RequestBusiness');
		$result = $requestBusiness->selectRequestsByUserId($user->getUserId());
		
		if ($result != NULL) {
		
			if(is_array($result) && sizeof($result) > 0) {
					
				header("Status: 200 Success", true, 200);
				
			} else {
				header("Status: 404 Not Found", true, 404);
			} 
		}
		else {
			header("Status: 500 Internal Server Error", true, 500);
		} 
		
		return $result;
	}
	
?>